import {Component,OnInit,ViewChild} from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
import { ActivatedRoute } from '@angular/router';
declare var $:any;
@Component({
  selector: 'doormodel',
  templateUrl: './doormodel.component.html',
  styleUrls: ['./doormodel.component.css'],
  animations: [
      flyIn
  ]
})
export class Doormodel implements OnInit{  
  @ViewChild('myModal') public myModal:ModalDirective;
  proData:any;
  proname:any;
  prodes:any;
  product_name:any;
  product_description:any;
  door_number:any;
  productdescription:any;
  product_id:any; 
  doordata = [];
  btype:number;
  ydoordata:any;
  constructor(private myHttp:MyHttp,private activeRoute: ActivatedRoute) {}
  ngOnInit() {   
    //this.getdata();
    this.activeRoute.params.subscribe(
      params=>{
        this.product_id = params['productId'];        
        this.getoneproduct();  
        this.getdata();
      }

    ); 
  }

  cancel(){
    this.door_number="";
   
  }

  comfirmnumber(){    

   if(this.door_number >120){
      this.door_number = 120;
    }
    this.doordata = [];
    for (let i = 0; i < this.door_number; i++) {
      this.doordata.push({ "door_name": 'door_name' + i, "door_size": '', "door_charge": '' });
    }
    this.myModal.hide();   

  }


  savealldoor(){
    this.myHttp.post('template/save',{
        "product_id":this.product_id,
        "data":this.doordata
    }).subscribe(res=>{       
      if(res.errorCode==0){
        alert(res.message);
        this.getdata();        
      }   
      
    },err=>{alert('保存失败')});
    
  }
  deleteDoor(i){
    this.doordata.splice(i,i+1);
  }
  getdata(){
    this.myHttp.post('template/findByProductId',{
        "product_id":this.product_id  
    }).subscribe(res=>{       
      if(res.errorCode==0){
        this.ydoordata = res.message;
      }   
      
    })
  }


  getoneproduct(){
    this.myHttp.post('product/findById',{
        "product_id":this.product_id
    }).subscribe(res=>{       
      if(res.errorCode==0){
        this.proData = res.message;
      }   
      
    })
  }



}

